const Router = require("koa-router")
const Server = require('./server')
const Check = require('./check')
const { koaBody } = require('@config')
const { auth } = require('@/lib/auth')

/**
 * 路由对象
 * @type {Router}
 */
const router = new Router({
  prefix: '/auth'
})

/**
 * 登录接口
 */
router.post("/login", koaBody(), async (ctx) => {
  Check.login(ctx)
  const data = await Server.login(ctx)
  ctx.sendSuccess(data)
})

/**
 * 退出登录
 */
router.get("/log-out", async (ctx) => {
  Check.logOut(ctx)
  await Server.logOut(ctx)
  ctx.sendSuccess('', '退出成功')
})

/**
 * 获取用户信息
 */
router.get("/get-user-info", auth, koaBody(), async (ctx) => {
  Check.getUserInfo(ctx)
  const data = await Server.getUserInfo(ctx)
  ctx.sendSuccess(data)
})

/**
 * 添加用户
 */
router.post("/add-user", auth, async (ctx) => {
  Check.addUser(ctx)
  const data = await Server.addUser(ctx)
  ctx.sendSuccess(data)
})

/**
 * 修改用户数据
 */
router.post("/modify", auth, koaBody(), async (ctx, next) => {
  Check.modify(ctx)
  const data = await Server.modify(ctx)
  ctx.sendSuccess(data)
})

module.exports = router




